import db from '../db/index.js'
export function getteacherlist(req, res) {
    const { pagesize, pagenum } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select  teacher.teacherid,teacher.name,sex,IDcard,phone,nationality,submajor.name as major_name from teacher inner join submajor on teacher.subid=submajor.submajorid  limit ${pagesize} offset ${page}`
    db.query(sql, (err, data) => {
        if (err) return res.dbno(err)
        const studata = data
        const sql2 = 'SELECT COUNT(*) as total FROM teacher'
        db.query(sql2, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, data: studata, total: data[0].total })
        })

    })
}
export function seachteacher(req, res) {
    const { condition, val, pagesize, pagenum } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select   teacher.teacherid,teacher.name,sex,IDcard,phone,nationality,submajor.name as major_name from teacher inner join submajor on teacher.subid=submajor.submajorid where ${condition}= ?  limit ${pagesize} offset ${page}`
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        const studata = data
        const sql2 = `SELECT COUNT(*) as total FROM teacher inner join submajor on teacher.subid=submajor.submajorid where ${condition}=? `
        db.query(sql2, val, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, data: studata, total: data[0].total })
        })
    })
}
export function teacherinfo(req, res) {
    const {teacherid} = req.query
    const sql  =`select teacher.teacherid,teacher.name,create_time,submajor.name as major_name,job.jobname as job_name from teacher 
    inner join submajor on teacher.subid=submajor.submajorid
    inner join job on teacher.jobid=job.jobid where teacher.teacherid = ?
    `
    db.query(sql,teacherid,(err,data)=>{
        if(err) return res.dbno(err)
            const teacherinfo = data.length>0?data[0]:[]
        const sql2 = 'select name,create_time from thonor where teacherid = ?'
        db.query(sql2, teacherid, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ data:{teacherinfo,honorlist:data}, status: 200 })
        })

    })
}
export function specialteacher(req, res) {
    const sql = `select teacher.teacherid,teacher.name,submajor.name as major_name ,tecspecial.create_time as create_time ,tecspecial.reson as reson ,tecspecial.end_time as end_time from teacher
     inner join submajor on teacher.subid = submajor.submajorid 
    inner join job on teacher.jobid = job.jobid
    inner join tecspecial on teacher.teacherid = tecspecial.teacherid
    `
    db.query(sql, (err, data) => {
        if (err) return res.dbno(err)
        res.send({ data, status: 200 })
    })
}
export function seachspecialteacher(req, res) {
    const { condition, val } = req.body
    const sql = `select teacher.teacherid,teacher.name,submajor.name as major_name ,tecspecial.create_time as create_time ,tecspecial.reson as reson ,tecspecial.end_time as end_time from teacher
     inner join submajor on teacher.subid = submajor.submajorid 
    inner join job on teacher.jobid = job.jobid
    inner join tecspecial on teacher.teacherid = tecspecial.teacherid where ${condition}=? `
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        res.send({ status: 200, data })
    })
}

